package com.example.myapplication.model.rx;

import android.app.Dialog;

import com.example.myapplication.R;
import com.example.myapplication.app.App;
import com.example.myapplication.utils.NetWorkUtils;

import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class RxSubscriber<T> implements Observer<T> {

    private Dialog showDialog;
    public RxSubscriber( Dialog showDialog) {
        this.showDialog=showDialog;
    }

    @Override
    public void onSubscribe(Disposable d) {
        showDialog.setOnCancelListener(dialog1 -> {
            dialog1.dismiss();
            d.dispose();
        });
    }

    @Override
    public void onNext(T value) {
        _onNext(value);
    }

    @Override
    public void onError(Throwable e) {

            //网络
            if (!NetWorkUtils.isNetConnected(App.getInstance())) {
                _onError(App.getInstance().getString(R.string.no_net));
            }
            //服务器
            else if (e instanceof UnknownHostException) {
                _onError(App.getInstance().getString(R.string.no_net));
            }
            //其它
            else {
                _onError(App.getInstance().getString(R.string.net_error));
            }
        showDialog.dismiss();
    }
    @Override
    public void onComplete() {

    }
    protected abstract void _onNext(T t);

    protected abstract void _onError(String message);

}
